<template>
  <div class="box">
    <div class="head">
      <div class="back">
        <el-button type="primary" size="mini" icon="el-icon-s-home" @click="back">返回</el-button>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePopup">筛选</el-button>
        <el-button type="primary" size="mini" icon="el-icon-s-data" @click="analysis">分析</el-button>
        <el-button type="primary" size="mini" icon="el-icon-s-data" @click="downLoad">导出</el-button>
        <el-button v-if="hasAdmin" type="primary" size="mini" icon="el-icon-s-data" @click="del">删除</el-button>
      </div>
      <div>
        <el-button type="success" size="mini" plain>共检索{{ total }}条</el-button>
      </div>
    </div>
    <searchScreen @submit-init="watchChange" v-if="openDialog" ref="searchScreen"/>
    <el-table :header-cell-style="{ background: '#eee', color: 'black' }" :height="tableHeight" size="mini" ref="table"
              :data="tableData" @select-all="selectAll" @selection-change="selectionChange" style="
        width: 100%;
        border: 1px solid #ebebeb;
        box-shadow: 4px 4px 4px #efeeee;
        border-radius: 6px;
      ">
      <el-table-column type="selection" width="55"></el-table-column>

      <el-table-column align="left" prop="public_number" width="180" label="公开(公告)号">

        <template slot-scope="scope">
          <!-- <el-checkbox @change="selectChange" v-model="scope.row.selected"></el-checkbox> -->
          <span style="border-bottom: 1px solid red; margin-left: 12px" @click="rowClick(scope.row)">{{ scope.row.public_number }}</span>
          <el-tag style="margin-left: 15px" size="mini">{{
            scope.row.legal_status
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="left" prop="enterprise" label="所属板块"/>
      <el-table-column align="left" prop="country_stat" label="国家名称"/>
      <el-table-column align="left" prop="province_stat" label="省份名称"/>
      <el-table-column align="left" prop="city_stat" label="城市名称"/>
      <el-table-column align="left" prop="is_internal" label="境内/境外"/>
      <el-table-column align="left" prop="establish_date" label="企业成立日期"/>
      <el-table-column align="left" prop="applicants" label="当前申请人"/>
      <el-table-column align="left" prop="applicants_stand_stat" label="申请人标准化"/>
      <el-table-column align="left" prop="application_number" label="申请号"/>
      <el-table-column align="left" prop="application_number_stand" label="申请号标准化"/>
      <el-table-column align="left" prop="inventors" label="发明人"/>
      <el-table-column align="left" prop="assignees" label="当前专利权人"/>
      <el-table-column align="left" prop="application_date" label="申请日"/>
      <el-table-column align="left" prop="legal_status" label="法律状态"/>
      <el-table-column align="left" label="是否高价值专利">
        <template slot-scope="scope">
          {{ scope.row.is_high_cost == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" prop="title" label="标题"/>
      <el-table-column align="left" prop="patent_score" label="专利估值"/>
      <el-table-column align="left" prop="inventors_count" label="发明人个数"/>

      <el-table-column align="left"  label="高价值专利要素-战略性新兴产业">
        <template slot-scope="scope">
          {{ scope.row.is_strategic_emerging == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="高价值专利要素-海外同族">
        <template slot-scope="scope">
          {{ scope.row.has_abroad_family == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="高价值专利要素-发明维持超过10年">
        <template slot-scope="scope">
          {{ scope.row.is_duration_ten_year == 0 ? "否" : "是" }}
        </template>
      </el-table-column>

      <el-table-column align="left" label="高价值专利要素-较高质押融资金额">
        <template slot-scope="scope">
          {{ scope.row.is_high_inventory == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="高价值专利要素-获得国家科学技术奖或中国专利奖">
        <template slot-scope="scope">
          {{ scope.row.has_reward == 0 ? "否" : "是" }}
        </template>
      </el-table-column>

      <el-table-column align="left" prop="public_date" label="公开(公告)日"/>
      <el-table-column align="left" prop="public_date" label="公开(公告)日"/>

      <el-table-column align="left" prop="ipc_main" label="IPC主分类号"/>
      <el-table-column align="left" prop="patent_score" label="专利评分"/>
      <el-table-column align="left" prop="duration_year" label="维持年限"/>
      <el-table-column align="left" prop="agency" label="代理机构"/>
      <el-table-column align="left" prop="patent_type" label="专利类型"/>
      <el-table-column align="left" prop="licensed_count" label="许可次数"/>
      <el-table-column align="left" prop="transfer_count" label="转让次数"/>
      <el-table-column align="left" prop="pledges_count" label="质押次数"/>
      <el-table-column align="left" prop="cited_count" label="被引用次数"/>
      <el-table-column align="left" label="是否复审">
        <template slot-scope="scope">
          {{ scope.row.has_recheck == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" prop="patent_invalidation_count" label="专利被提起无效次数"/>
      <el-table-column align="left" label="专利是否被宣告无效">
        <template slot-scope="scope">
          {{ scope.row.is_patent_invalidation == 0 ? "否" : "是" }}
        </template>
      </el-table-column>

      <el-table-column align="left" prop="reward_name" label="中国专利奖项名称"/>
      <el-table-column align="left" prop="region_reward_name" label="地方专利奖项名称"/>
      <el-table-column align="left" label="是否涉诉专利">
        <template slot-scope="scope">
          {{ scope.row.has_lawsuit == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="是否转让出去">
        <template slot-scope="scope">
          {{ scope.row.is_transferred == 0 ? "否" : "是" }}
        </template>
      </el-table-column>




    </el-table>
    <div class="page">
      <el-pagination @current-change="currChange" :page-size="pageSize" align="end" background
                     layout="prev, pager, next" :total="total" :current-page="index"/>
    </div>
  </div>
</template>

<script>
  import {
    toRetrieval,
    deletePatent,
    downLoadPatent
  } from "@/api/retrieval";

  import searchScreen from "./searchScreen.vue";
  import {
    mapGetters
  } from 'vuex'

  export default {
    data() {
      return {
        openDialog: false,
        tableHeight: window.innerHeight - 160,
        total: 0,
        pageSize: 20,
        index: 1,
        tableData: [],
        selectList: [], // 需导出来的数据

        isAll: false,
        currentPageSize: 0, //当前页 的总数

        hasAdmin: true

      };
    },
    components: {
      searchScreen,
    },
    computed: {
      ...mapGetters([
        'roles'
      ])
    },
    mounted() {
      if (this.roles.indexOf('admin') > -1) {
        this.hasAdmin = true
      } else {
        this.hasAdmin = false
      }

      let data = localStorage.getItem("searchData");
      localStorage.setItem("searchIndex", this.index);
      window.onresize = () => {
        //    console.log("1")
        this.tableHeight = window.innerHeight - 160;
      };
      if (data) {
        toRetrieval(JSON.parse(data)).then((res) => {
          // console.log("数据", res.data)
          let {
            total,
            values
          } = res.data;
          this.total = total;

          this.tableData = this.formatList(values);
          this.currentPageSize = this.tableData.length
          localStorage.setItem("total", this.total);
          localStorage.setItem("tableData", JSON.stringify(this.tableData));
        });
      }
    },
    methods: {
      currChange(index) {
        this.index = index;
        localStorage.setItem("searchIndex", this.index);
        // console.log(index)
        let data = JSON.parse(localStorage.getItem("searchData"));
        data.pageIndex = this.index;

        toRetrieval(data).then((res) => {
          // console.log("数据", res.data)
          let {
            total,
            values
          } = res.data;
          this.total = total;
          this.tableData = this.formatList(values);
          if (this.isAll) {
            this.$refs.table.toggleAllSelection(true)

          }

        });

        //   if(this.isAll){

        //   }

      },
      del() {
        let list = this.selectList
        if (list.length > 0) {
          console.log('patent', list)
          //  deletePatent()
          let arr = []
          list.forEach(item => {
            arr.push(item.public_number)
          })
          deletePatent({
            publicNumbers: arr
          }).then(res => {
            let data = localStorage.getItem("searchData");

            this.$message({
              message: '删除成功',
              type: 'success'
            })

            toRetrieval(JSON.parse(data)).then((res) => {
              // console.log("数据", res.data)
              let {
                total,
                values
              } = res.data;
              this.total = total;

              this.tableData = this.formatList(values);
              this.currentPageSize = this.tableData.length
              localStorage.setItem("total", this.total);
              localStorage.setItem("tableData", JSON.stringify(this.tableData));
            });
          })

        } else {
          this.$message({
            message: '请勾选要删除的数据',
            type: 'warning'
          })
        }
      },
      selectionChange(selection) {
        this.selectList = selection
        //    if(this.currentPageSize == selection.length){
        //        this.isAll=true
        //    }else{
        //        this.isAll=false
        //    }

      },
      selectAll(selection) {
        if (selection.length == 0) {
          this.isAll = false
        } else {
          this.isAll = true
        }
      },

      downLoad() {
        if (this.selectList.length == 0) {
          this.$message({
            message: '请勾选要导出的数据',
            type: 'warning'
          })
        } else {
          console.log("123", this.selectList)
          // isAll 为true  导出 全部
          let data = []
          if (this.isAll) {
            data = {
              isWhole: 1
            }
          } else {
            let arr = []
            let list = this.selectList
            list.forEach(item => {
              arr.push(item.public_number)
            })

            data = {
              isWhole: 0,
              publicNumbers: arr

            }
          }

          this.$showLoading();

          downLoadPatent(data).then(res => {

            this.doDownload(res)

            this.$hideLoading();
          }).catch(res => {
          })

        }

      },
      doDownload(res) {
        const headers = res.headers
        let link = document.createElement("a");
        // 响应头获取编码的文件名称
        let filename = decodeURI(headers["content-disposition"].split(";")[1].split("=")[1])
        let blob;
        try {
          //该实例化的方式第一个参数必须是数组的格式
          blob = res.data;
        } catch (e) {
          //旧版本浏览器下的blob创建对象
          window.BlobBuilder = window.BlobBuilder ||
            window.WebKitBlobBuilder ||
            window.MozBlobBuilder ||
            window.MSBlobBuilder;
          if (e.name == 'TypeError' && window.BlobBuilder) {
            var blobbuilder = new BlobBuilder();
            BlobBuilder.append(data);
            blob = blobbuilder.getBlob('application/vnd.ms-excel,charset=utf-8');
          } else {
            alert("浏览器版本较低，暂不支持该文件类型下载");
          }
        }
        let url = window.URL.createObjectURL(blob);

        link.setAttribute("href", url)
        link.download = filename
        document.body.appendChild(link);
        link.click()
        document.body.removeChild(link);
      },

      watchChange(data) {
        this.total = data.total;
        this.tableData = this.formatList(data.values);
      },
      back() {
        this.$router.go(-1);
      },
      formatList(list) {
        let id = 0;
        list.forEach((item) => {
          id = id + 1;
          item.id = id;
        });
        return list;
      },
      rowClick(row) {
        this.$router.push({
          path: "/retrieval/detail",
        });

        localStorage.setItem("resultItem", JSON.stringify(row));
        //   console.log("row", this.tableData)
      },
      analysis() {
        if (this.tableData.length == 0) {
          this.$message({
            message: "暂无数据",
            type: "warning",
          });
        } else {
          this.$router.push({
            path: "/retrieval/analysis",
          });
        }
      },
      handlePopup() {
        this.openDialog = true;
        this.$nextTick(() => {
          this.$refs.searchScreen.init();
        });
      },

    },
  };
</script>

<style lang="scss" scoped>
  .box {
    padding: 28px;
    padding-top: 0px;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #f9f9f9;

    .head {
      height: 44px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;

      .back {
        // height: 100%;
        // width: 130px;
        // background-color: #ebebeb;
      }
    }

    .page {
      border: 1px solid #ebebeb;
      margin-top: 8px;
      border-radius: 6px;
      background-color: #ffffff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      height: 42px;
      box-shadow: 4px 4px 4px #efeeee;
    }
  }
</style>
